<?php
/**
 * DownGoods.php class file.
 * @author wudg <544038230@qq.com>
 * @date 2020/12/27- 下午5:40
 */

namespace common\jobs\sync;


use common\components\api\isv\Goods;
use common\models\PddMallProduct;
use common\exception\JobException;
use common\helpers\SendTaskHelper;
use GuzzleHttp\Exception\GuzzleException;
use common\exception\ServiceException;
use common\dao\PddSyncBatchLogDao;
use common\helpers\JobHelper;
use common\drivers\queue\Manager;
use Yii;
use yii\base\Exception;

class SyncGoodsToSyncbatch extends SyncBase implements \common\drivers\interfaces\JobInterface
{
    public $args = [];


    /**
     * 同步每页请求条数
     * @var int
     */
    public $limit = 100;

    /**
     * 获取异步任务执行需要的参数
     * SyncProduct constructor.
     * @param array $args
     */
    public function __construct(array $args = [])
    {
        $this->args = $args;
        $this->totalCacheKey .= ('batch' . $this->args['batch_log_id']);
    }


    /**
     * 处理队列
     * @return mixed|void
     */
    public function handle() :void
    {
        try {

            Yii::$app->db->open();
            $syncBatchLogDao = new PddSyncBatchLogDao();
            //同步在售商品
            $ret = $syncBatchLogDao->getLastLogInfo($this->args['batch_log_id']);
            $ids = $ret->ids;
            $idArr = explode(',',$ids);
            if($idArr){
                foreach($idArr as $k=>$v){
                    //获取商品ID
                    $goods_info = $goodsDetail = PddMallProduct::find()
                        ->where(['goods_id' => $v])
                        ->one()->toArray();
                    if($goods_info){
                        $res = $syncBatchLogDao->insertDataProducts([
                            'batch_no' => $this->args['batch_log_id'],
                            'type' => $ret->source_type,
                            'batchType' => $ret->batchType,
                            'uid' =>  $ret->uid,
                            'mall_id' =>  $goods_info['mall_id'],
                            'goods_id' => $v,
                            'created_at' => time(),
                        ]);
//                        SendTaskHelper::toSend([
//                            'taskName'=>'SyncProductbatchDetail',
//                            'args'=>[
//                                'goods_id' => $v,
//                                'batch_log_id' => $this->args['batch_log_id'],
//                                'batch_log_info_id' => $res->id,
//                                'mall_id' => $goods_info['mall_id'],
//                                'uid' => $ret->uid,
//                            ]
//                        ]);
//                        JobHelper::toSyncBatchDetail([
//                            'goods_id' => $v,
//                            'batch_log_id' => $this->args['batch_log_id'],
//                            'batch_log_info_id' => $res->id,
//                            'mall_id' => $goods_info['mall_id'],
//                            'uid' => $ret->uid,
//                        ]);
                        $spider = new SyncGoodsToSyncbatchdetail([
                            'goods_id' => $v,
                            'batch_log_id' => $this->args['batch_log_id'],
                            'batch_log_info_id' => $res->id,
                            'mall_id' => $goods_info['mall_id'],
                            'uid' => $ret->uid,
                        ]);
                        Manager::factory(Yii::$app->params['queue']['syncBatchDetail'])->push($spider);
                    }

                }
            }
            Yii::$app->redis->setex($this->totalCacheKey,86400,$ret->total_cnt);
            Yii::$app->db->close();
        }catch (ServiceException $e)
        {
            throw new JobException($e->getMessage());
        }catch (Exception $e)
        {
            throw new JobException($e->getMessage());
        }catch (GuzzleException $e)
        {
            throw new JobException($e->getMessage());
        }
    }

}